use std::collections::HashMap;
use sysinfo::{System };

// 系统信息在此显示
pub struct MyInfo {
    system: System,
}

impl MyInfo {
    pub fn new() -> MyInfo {
        MyInfo{
            system: System::new_all()
        }
    }


    pub fn get_memory_all(&mut self) -> HashMap<&'static str, u64> {
        self.system.refresh_all();
        let total_memory =  self.system.total_memory();
        let used_memory = self.system.used_memory();
        let total_swap = self.system.total_swap();
        let used_swap = self.system.used_swap();

        let mut map = HashMap::new();
        map.insert("total_memory", total_memory);
        map.insert("used_memory", used_memory);
        map.insert("total_swap", total_swap);
        map.insert("used_swap", used_swap);

        map
    }

}

#[tauri::command]
pub fn get_memory_all() -> HashMap<&'static str, u64> {
    MyInfo::new().get_memory_all()
}


#[cfg(test)]
mod tests {
    use crate::system_info::MyInfo;

    #[test]
    fn test_get_memory_all() {
        let mut info = MyInfo::new();
        assert_eq!(info.get_memory_all().len(), 4);
    }
}